<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 报价单
 */

class billCost
{

    private $dbca;

    public function __construct(&$dbca)
    {
        $this->dbca = $dbca;
    }


    /**
     * 查询报价单
     */
    public function searchBillCost(&$arg_array)
    {

        $sql = '';

        //订单号
        if (isset($arg_array['costCode']) && !empty($arg_array['costCode'])) {
            $sql .= " AND bc.costCode LIKE '%" . trim($arg_array['costCode']) . "%'";
        }

        if (isset($arg_array['costName']) && !empty($arg_array['costName'])) {
            $sql .= " AND bc.costName LIKE '%" . trim($arg_array['costName']) . "%'";
        }

        if (isset($arg_array['companyId']) && !empty($arg_array['companyId'])) {
            $sql .= " AND cm.company_id = " . $arg_array['companyId'];
        }

        if (isset($arg_array['companyName']) && !empty($arg_array['companyName'])) {
            $sql .= " AND cm.company_name LIKE '%" . trim($arg_array['companyName']) . "%'";
        }

        if (isset($arg_array['startDate']) && !empty($arg_array['startDate'])) {
            $sql .= " AND bc.createTime >= '" . $arg_array['startDate'] . "'";
        }

        if (isset($arg_array['endDate']) && !empty($arg_array['endDate'])) {
            $sql .= " AND bc.createTime <= '" . $arg_array['endDate'] . " 23:59:59'";
        }

        if (isset($arg_array['costStatus']) && !empty($arg_array['costStatus'])) {
            $sql .= " AND bc.costStatus IN(" . $arg_array['costStatus'] . ")";
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {
            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {
                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM bill_cost bc
                            INNER JOIN company_master cm ON bc.companyId=cm.company_id ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }
                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
                else{
                    $sql .= ' ORDER BY bc.createTime DESC ';
                }

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
            }else{
                $sql .= ' ORDER BY bc.createTime DESC ';
            }

            $search_sql = 'SELECT bc.*, cm.company_name,um.trueName,um.mobile AS userMobile,ss.sendTypeName
                          FROM bill_cost bc
                          INNER JOIN company_master cm ON bc.companyId=cm.company_id 
                          LEFT JOIN user_master um ON um.userID = bc.creator 
                          LEFT JOIN sys_sendtype ss ON bc.expressId=ss.sendTypeID ' . $sql;

            $item_array = &$this->dbca->stmt_query($search_sql);

            return $item_array;

        } catch (Exception $e) {
            error_log($e->getMessage());
            return null;
        }
    }

    //查看详情
    public function searchCostDtl($code)
    {
        //查询分类信息
        $search_sql = "SELECT bcd.*, gs.bigCategoryID, gs.bigCategoryName 
                      FROM bill_cost_dtl bcd
                      INNER JOIN goods_sale gs ON bcd.goodsCode=gs.postId
                      WHERE costCode = '" . $code . "'";

        $item_array = &$this->dbca->stmt_query($search_sql);
        return $item_array;
    }

    //修改报价单状态
    public function changeCostStatus($param)
    {
        $bill_cost = new entity('bill_cost');
        $bill_cost->costCode = $param['costCode'];
        if (!$this->dbca->fetch($bill_cost)) {
            return array('code' => 400, 'message' => '未找到报价单信息');
        }
        $bill_cost->costStatus = $param['status'];
        $this->dbca->save($bill_cost);
        return array('code' => 200, 'data'=>$bill_cost);
    }

    //保存报价单
    public function saveCost($param)
    {
        $bill_cost = new entity('bill_cost');
        if(isset($param['costCode']) && !empty($param['costCode'])){
            $bill_cost->costCode = $param['costCode'];
            if(!$this->dbca->fetch($bill_cost)){
                return array('code' => 400, 'message' => '未找到报价单信息');
            }
        }else {
            $bill_cost->costCode = generate_seqno('seqno_cost', 'C');
            $bill_cost->creator = $param['operator'];
        }

        $bill_cost->costStatus = 0;
        $bill_cost->companyId = $param['companyId'];
        $bill_cost->costName = $param['costName'];
        $bill_cost->area = $param['area'];
        $bill_cost->categoryName = $param['categoryName'];
        $bill_cost->expressId = $param['expressId'];
        $bill_cost->remark = $param['remark'];
        $bill_cost->validDate = $param['validDate'];


        $this->dbca->beginTransaction();

        try{
            $this->dbca->save($bill_cost);
            if(isset($param['costCode']) && !empty($param['costCode'])) {
                $this->dbca->stmt_query('DELETE FROM bill_cost_dtl WHERE costCode = ?', 's', $param['costCode']);
            }
            foreach ($param['dtl'] as $goods) {
                $dtl = new entity('bill_cost_dtl');
                $dtl->creator = $param['operator'];
                $dtl->costCode = $bill_cost->costCode;
                $dtl->goodsCode = $goods['goodsCode'];
                $dtl->goodsName = $goods['goodsName'];
                $dtl->salePrice = $goods['salePrice'];
                $dtl->discount = $goods['discount'];
                $dtl->price = $goods['price'];
                $dtl->useRate = $goods['useRate'];
                $dtl->packageId = $goods['packageId'];
                $dtl->packageName = $goods['packageName'];
                $dtl->specId = $goods['specId'];
                $dtl->specName = $goods['specName'];
                $dtl->remark = $goods['remark'];
                $this->dbca->save($dtl);
            }
            $this->dbca->commit();
            return array('code' => 200 , 'message' => 'ok');

        }catch (Exception $e){
            elog($e->getMessage());
            $this->dbca->rollBack();
        }
        return array('code' => 500 , 'message' => '系统错误，保存失败');

    }
}